<?php

function reequipment_admin_schedule()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:form:create:delete:edit:update', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('schedule_id', 'int:1:', $schedule_id, '', XARVAR_NOT_REQUIRED)) return;    
    if (!xarVarFetch('model_id', 'int:1:', $model_id, '', XARVAR_NOT_REQUIRED)) return;    
    if (!xarVarFetch('model_id_2', 'int:1:', $model_id_2, '', XARVAR_NOT_REQUIRED)) return; 
    if (!xarVarFetch('model_id_3', 'int:1:', $model_id_3, '', XARVAR_NOT_REQUIRED)) return; 
    if (!xarVarFetch('area_id', 'int:1:', $area_id, '', XARVAR_NOT_REQUIRED)) return;    
    if (!xarVarFetch('frequency_id', 'int:1:', $frequency_id, '', XARVAR_NOT_REQUIRED)) return;   
    if (!xarVarFetch('downtime', 'checkbox', $downtime, false, XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('issue', 'str:1:', $issue, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('item_ids', 'str:1:', $item_ids, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('manhours', 'float', $manhours, '', XARVAR_NOT_REQUIRED)) return;   
    
    if (!xarSecurityCheck('AdminEquipment', 1)) return;
    $data = array();
    
    switch(strtolower($phase)) {
        case 'form':
            $data['models'] = xarModAPIFunc('reequipment', 'user', 'getmodels');
            $data['areas'] = xarModAPIFunc('reequipment', 'user', 'getareas');
            $data['frequency'] = xarModAPIFunc('reequipment', 'user', 'getfrequency');            
            $data['schedules'] = xarModAPIFunc('reequipment', 'user', 'getschedule');
            foreach ($data['schedules'] as $key => $item) {
        		$data['schedules'][$key]['issue'] = substr($item['issue'], 0, 40);
    		}
            // return var_export($data['schedules']);
            $data['form_label'] = xarML('Add a Schedule');
            $data['form_model_name'] = '';
            $data['form_phase'] = 'create';
            $data['form_model_id'] = '';
            $data['form_model_id_2'] = '';
            $data['form_model_id_3'] = '';
            $data['form_area_id'] = '';
            $data['form_schedule_id'] = '';
            $data['form_frequency_id'] = '';
            $data['form_issue'] = '';
            $data['form_item_ids'] = '';
            $data['form_manhours'] = '';
            $data['form_downtime'] = '';
        break;

        case 'create':
        		if (empty($issue)) {
        			xarResponseRedirect(xarModURL('reequipment', 'admin', 'schedule'));
        		}
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createschedule', array('area_id' => $area_id, 'model_id' => $model_id, 'frequency_id' => $frequency_id, 'issue' => $issue, 'item_ids' => $item_ids, 'manhours' => $manhours, 'downtime' => $downtime));
        		if (!empty($model_id_2)){
        			$postinfo = xarModAPIFunc('reequipment', 'admin', 'createschedule', array('area_id' => $area_id, 'model_id' => $model_id_2, 'frequency_id' => $frequency_id, 'issue' => $issue, 'item_ids' => $item_ids, 'manhours' => $manhours, 'downtime' => $downtime));
        		} elseif (!empty($model_id_3)){
        			$postinfo = xarModAPIFunc('reequipment', 'admin', 'createschedule', array('area_id' => $area_id, 'model_id' => $model_id_3, 'frequency_id' => $frequency_id, 'issue' => $issue, 'item_ids' => $item_ids, 'manhours' => $manhours, 'downtime' => $downtime));
        		}
        		xarResponseRedirect(xarModURL('reequipment', 'admin', 'schedule'));
        break;
         
        case 'delete':
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'deleteschedule', array('schedule_id' => $schedule_id));
                xarResponseRedirect(xarModURL('reequipment', 'admin', 'schedule'));
        break;  
        
        case 'edit':
            $data['models'] = xarModAPIFunc('reequipment', 'user', 'getmodels');
            $data['areas'] = xarModAPIFunc('reequipment', 'user', 'getareas');
            $data['frequency'] = xarModAPIFunc('reequipment', 'user', 'getfrequency');            
            $data['schedules'] = xarModAPIFunc('reequipment', 'user', 'getschedule');
            foreach ($data['schedules'] as $key => $item) {
        		$data['schedules'][$key]['issue'] = substr($item['issue'], 0, 40);
    		}
            
            $data['form_label'] = xarML('Edit a Area');
            $form_var = xarModAPIFunc('reequipment', 'user', 'getschedule', array('schedule_id' => $schedule_id));
            foreach($form_var as $var){
            	$data['form_schedule_id'] = $var['schedule_id'];
            	$data['form_issue'] = $var['issue'];
            	$data['form_model_id'] = $var['model_id'];
            	$data['form_area_id'] = $var['area_id'];
            	$data['form_frequency_id'] = $var['frequency_id'];
            	$data['form_manhours'] = $var['manhours'];
            	$data['form_downtime'] = $var['downtime'];
            }
            $data['areas'] = xarModAPIFunc('reequipment', 'user', 'getareas');
            $data['form_phase'] = 'update';
            $data['form_model_id_2'] = '';
            $data['form_model_id_3'] = '';
        break;
        
		case 'update':
			// return var_export($manhours);
			if (empty($issue)) {
				xarResponseRedirect(xarModURL('reequipment', 'admin', 'schedule'));
			}
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateschedule', array('schedule_id' => $schedule_id, 'area_id' => $area_id, 'model_id' => $model_id, 'frequency_id' => $frequency_id, 'issue' => $issue, 'item_ids' => $item_ids, 'manhours' => $manhours, 'downtime' => $downtime));
			xarResponseRedirect(xarModURL('reequipment', 'admin', 'schedule'));
        break;
        
    }

    // Return the output
    return $data;
}

?>